package com.app.controller.backstage;

import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.model.UserModel;
import com.app.service.AdminUserService;
import com.app.service.MyService;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@RequestMapping("/admin/user")
public class UserAdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private MyService myService;
    @Autowired
    private AdminUserService adminUserService;
    //查询用户列表 关键字姓名或者手机号
    @GetMapping("/selectUserList")
    public Result<Object> selectUserList(Integer page, Integer limit, String name,Integer villageId,Integer auditStatus,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (user.getVillageId() != 0) {
            return Result.success(userService.selectUserList(page, limit, name,user.getVillageId(),auditStatus));
        }
        return Result.success(userService.selectUserList(page, limit, name,villageId,auditStatus));
    }

    //查询用户积分详情
    @GetMapping("/selectIntegral")
    public Result<Object> selectIntegral(Integer id, Integer page, Integer limit) {
        return Result.success(myService.selectMyIntegral(id, page, limit));
    }

    //查询用户信息
    @GetMapping("/selectUserById")
    public Result<Object> selectUserById(Integer id) {
        return Result.success(userService.selectUserById(id));
    }

    //用户实名审核
    @GetMapping("/updateAuditStatus")
    public Result<Object> updateAuditStatus(Integer id, Integer auditStatus,String remark) {
        userService.updateAuditStatus(id,auditStatus,remark);
        return Result.success("操作成功");
    }
}
